<template>
	<view class="content">
		<!-- <image class="loginLogo"
			src="https://beauty-of-face.oss-cn-shanghai.aliyuncs.com/H5/homeLogo.png" mode="">
		</image> -->
		<!-- <p class="loginLogo"> </p> -->
		<image class="loginLogo" src="/static/logo.png" mode=""></image>
		<p class="loginTitle">欢迎登陆</p>
		<form @submit="handleLogin">
			<view class="form-group">
				<label for="phoneNumber">账号</label>
				<input id="phoneNumber" v-model="phoneNumber" type="text" placeholder="请输入账号" />
			</view>
			<view class="form-group">
				<label for="password">密码</label>
				<input id="password" v-model="password" type="password" placeholder="请输入密码" />
			</view>
			<navigator class="leftText" url="/pages/my/login/codeLogin" hover-class="none">验证码登录</navigator>
			<navigator class="rightText" url="/pages/my/login/editPassword" hover-class="none">忘记密码？</navigator>
			<view class="buttonBox">
				<view class="button01" @click="handleLogin()">登录</view>
				<!-- <view class="button02" @click="goRegister()">注册</view> -->
			</view>
		</form>
	</view>
</template>

<script>
	import {
		Login
	} from '@/api/login.js';
	import {
		getUserInfo,
		getSilent
	} from '@/api/user.js';
	export default {
		data() {
			return {
				phoneNumber: '',
				password: '',
				userInfo: {},
				pid: -1
			};
		},
		onLoad() {},
		methods: {
			getUser() {
				getUserInfo().then(res => {
					this.userInfo = res.data
					console.log(this.userInfo, "this.userInfo")
					if (this.userInfo.nickName) {

						if (localStorage.getItem('pid')) {
							this.pid = localStorage.getItem('pid');
						}
						getSilent(this.pid).then(res => {
							window.location.replace(res.data);
						})
						// uni.switchTab({
						// 	url: '/pages/index/index'
						// });
					} else {
						uni.redirectTo({
							url: '/pages/my/setting/index'
						});
					}
				});
			},
			async handleLogin() {
				try {
					const response = await Login({
						phoneNumber: this.phoneNumber,
						password: this.password
					});
					console.log('登录成功:', response);
					if (response.code == 200) {
						localStorage.setItem('token', response.data)
						// uni.switchTab({
						// 	url: '/pages/index/index'
						// });
						if (localStorage.getItem('pid')) {
							this.pid = localStorage.getItem('pid');
						}
						getSilent(this.pid).then(res => {
							window.location.replace(res.data);
						})
						// getSilent().then(res => {
						// 	window.location.replace(res.data);
						// })
						// this.getUser()
					}
				} catch (error) {
					console.error('登录失败:', error);
				}
			},
			goRegister() {
				uni.navigateTo({
					url: '/pages/my/login/register'
				})
			}
		}
	};
</script>

<style lang="scss">
	@import './login.scss';
</style>